package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import com.zhiyou.aifeng.common.base.gone
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.base.visible
import com.zhiyou.aifeng.common.helper.formatDecimal
import com.zhiyou.aifeng.common.helper.get
import com.zhiyou.aifeng.common.helper.getString
import com.zhiyou.aifeng.common.helper.getTimeString
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.base.addItems
import com.zhiyou.aifeng.common_ui.base.loadLinear
import com.zhiyou.aifeng.common_ui.base.refresh
import com.zhiyou.aifeng.common_ui.model.CommonData
import com.zhiyou.aifeng.common_ui.utils.bindLifeCycle
import com.zhiyou.aifeng.common_ui.utils.subscribeBy
import com.zhiyou.aifeng.common_ui.utils.subscribeByFinally
import com.zhiyou.aifeng.model.ResultData
import com.zhiyou.aifeng.utils.setEmptyIcon
import com.zhiyou.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet_list.*
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import kotlinx.android.synthetic.main.layout_title_drop.*
import net.idik.lib.slimadapter.SlimAdapter

class WalletListActivity : BaseActivity() {

    private var mSelectDate: String = ""
    private val dateList = ArrayList<ResultData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet_list)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()

        getViewModel().getDateList()
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBy(
                onNext = { item ->
                    dateList.addItems(item)
                    if (dateList.any { it.isChecked }) {
                        val data = dateList.first { it.isChecked }
                        drop_right.text = data.hint

                        if (data.list.any { it.isChecked }) {
                            val inner = data.list.first { it.isChecked }
                            list_month.text = inner.hint
                            mSelectDate = inner.title
                            updateList()
                        }
                    }
                }
            )
    }

    override fun getViewModel() = get<WalletViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        drop_title.text = "收入记录"
        initList()

        drop_right_ll.oneClick {
            getViewModel().showDropFilter(
                this,
                drop_right,
                drop_arrow,
                wallet_divider,
                dateList
            ) { item ->
                if (item.list.any { it.isChecked }) {
                    val inner = item.list.first { item.isChecked }
                    list_month.text = inner.hint
                    mSelectDate = inner.title
                    updateList()
                }
            }
        }

        list_month_ll.oneClick {
            getViewModel().showDropFilter(
                this,
                list_month,
                list_arrow,
                wallet_divider2,
                dateList.first { it.isChecked }.list
            ) {
                mSelectDate = it.title
                updateList()
            }
        }
    }

    private fun initList() {
        empty_img.setEmptyIcon()
        empty_hint.text = "暂无相关收入信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh) {
                if (!isLoadingMore) {
                    isLoadingMore = true
                    getData(pageNum)
                }
            }

            mAdapter = SlimAdapter.create()
                .register<CommonData>(R.layout.item_income_list) { data, injector ->
                    injector.text(R.id.item_income_type, data.detail)
                        .text(R.id.item_income_amount, "+${data.amount.formatDecimal()}")
                        .text(R.id.item_income_time, data.create_date.getTimeString())
                }
                .attachTo(this)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getRecordList(
            index,
            getString("userId"),
            mSelectDate
        )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it.result.results)
                        if (it.result.results.count() > 0) pageNum++
                    }

                    list_total.text = "总收入${it.total.formatDecimal()}元"

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

    private fun updateList() {
        swipe_refresh.isRefreshing = true

        empty_view.gone()
        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        pageNum = 1
        getData(pageNum)
    }

}